#!/bin/sh

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
#       Copyright (C) 2012-4 Michael D. Taht, Toke Høiland-Jørgensen, Sebastian Moeller


[ -n "$IFACE" ] || exit 1

. /etc/sqm/sqm.conf
. ${SQM_LIB_DIR}/functions.sh
. ${SQM_LIB_DIR}/defaults.sh
STATE_FILE="${SQM_STATE_DIR}/${IFACE}.state"

if [ -z "${SCRIPT}" ] ; then
    sqm_error "SCRIPT value is not defined in /etc/sqm/${IFACE}.iface.conf"
    sqm_error "Please check your configuration and try again."
    exit 1
fi

[ -d "${SQM_STATE_DIR}" ] || mkdir -p "${SQM_STATE_DIR}"

if [ -f "${STATE_FILE}" ]; then
    sqm_error "SQM already activated on ${IFACE}."
    exit 1
fi

# in case of spurious hotplug events, try double check whether the interface is really up
if [ ! -d /sys/class/net/${IFACE} ] ; then
    sqm_error "${IFACE} does currently not exist, not even trying to start SQM on nothing."
    exit 1
fi

if [ "${ENABLED:-1}" -ne "1" ]; then
    sqm_log "SQM config disabled on ${IFACE}."
    exit 0
fi

if [ ! -f "${SQM_LIB_DIR}/$SCRIPT" ]; then
    sqm_error "SQM script ${SCRIPT} not found!"
    exit 1
fi

. "${SQM_LIB_DIR}/$SCRIPT"

sqm_trace; sqm_trace "$(date): Starting." # Add some space and a date stamp to verbose log output and log files to separate runs
sqm_log "Starting SQM script: ${SCRIPT} on ${IFACE}, in: ${DOWNLINK} Kbps, out: ${UPLINK} Kbps"
sqm_start && write_defaults_vars_to_state_file ${STATE_FILE} ${SQM_LIB_DIR}/defaults.sh && sqm_log "${SCRIPT} was started on ${IFACE} successfully"

exit 0
